#include <string.h>

#include "curl/curl.h"

#include "file_delete_manager.h"
#include "file_queue.h"
#include "cicd_config.h"
#include "cicd_common.h"

// curl -X DELETE --header 'Content-Type: application/json;charset=UTF-8' '

void file_delete_once() {
    CURL *curl = NULL;
    struct curl_slist *headers =  NULL;

    char token[35];
    char owner[20];
    char repo[30];
    char file[256];
    char sha[45];
    char url[512] = { 0 };

    int ret = queue_file_get(token, owner, repo, file, sha);
    if(ret != QUEUE_OK) {
        printf_info("No files need to be deleted\n");
        return;
    }

    delete_char(sha, strlen(sha), '\"');

    snprintf(url, sizeof(url), "%s/%s/%s/contents/%s?access_token=%s&sha=%s&message=clear", global_https_header, owner, repo, file, token, sha);

    headers = curl_slist_append(headers, global_request_header);

    curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, DELETE);
    curl_easy_setopt(curl, CURLOPT_HEADER, headers);
    curl_easy_perform(curl);
    printf_success("File deleted successfully\n");
    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);
}